class Solution {
public:
    vector<int> inventoryManagement(vector<int>& nums, int k) {
        qsort(nums,0,nums.size()-1,k);
        return {nums.begin(),nums.begin()+k};
    }
    void qsort(vector<int>& nums,int begin,int end,int k)
    {
        if(begin>=end) return;

        int key=nums[begin];

        int left=begin,right=end;
        int cur=left+1;
        //分为三块
        while(cur<=right)
        {
            if(nums[cur]<key)
            swap(nums[left++],nums[cur++]);
            else if(nums[cur]==key)
            cur++;
            else
            swap(nums[right--],nums[cur]);
        }
        // 判断
        int a=left-begin;
        int b=right-left+1;

        if(a>=k) qsort(nums,begin,left-1,k);
        else if(a+b>=k) return;
        else qsort(nums,right+1,end,k-a-b);
    }
};